package com.vnt.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vnt.entity.SysUser;
import com.vnt.mapper.UserMapper;
import com.vnt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements UserService, UserDetailsService {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("用户名:"+s);
        QueryWrapper<SysUser> query = new QueryWrapper<>();
        query.eq("user_name",s);
        SysUser one = userService.getOne(query);

        User u = new User(one.getUserName(), passwordEncoder.encode(one.getPassword()), AuthorityUtils.commaSeparatedStringToAuthorityList("admin,ROLE_admin"));
        return u;
    }
}